package com.ch.sys.model;

import java.util.HashMap;
import java.util.Map;

import com.ch.fastbuilder.common.GenerationType;
import com.ch.fastbuilder.common.PageResult;
import com.ch.fastbuilder.model.BuilderModel;
import com.ch.fastbuilder.model.Model;
import com.ch.sys.utils.ParamMap;

public class Role extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static String TABLE = "role";
	public static String ID = "id";
	public static String NAME = "name";
	public static String CREATED = "created";
	
	public Role() {
		super.setPrimaryKey(ID);
		super.setTableName(TABLE);
		//super.setGenerationType(GenerationType.UUID);
		super.setGenerationType(GenerationType.IDENTITY);
		super.setColumns(ID, NAME, CREATED);
	}
	
	public static PageResult findByPage(ParamMap params) {
		BuilderModel builder = Model.InitParams(params);
		PageResult pageResult = builder.findPage(Role.class);
		
		return pageResult;
	}
	
	public static Role getByEmployeeId(Long employeeId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("employee_id", employeeId);
		
		BuilderModel builder = Model.InitParams(params);
		builder.select("r.id, r.name, r.created");
		builder.from("employee_role emr");
		builder.innerJoin("role r on emr.role_id = r.id");
		builder.where("emr.employee_id = :employee_id");
		
		Role role = builder.get(Role.class);
		return role;
	}
}
